package com.hkts.naturalgas.dictionaries.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.naturalgas.datahandler.constance.EnviromentConstance;
import com.hkts.naturalgas.dictionaries.entity.HktsEnvironmentProtectionDictionaries;
import com.hkts.naturalgas.dictionaries.entity.HktsEnvironmentProtectionItem;
import com.hkts.naturalgas.dictionaries.entity.HktsFieldControl;
import com.hkts.naturalgas.dictionaries.service.IHktsEnvironmentProtectionDictionariesService;
import com.hkts.naturalgas.dictionaries.service.IHktsEnvironmentProtectionItemService;
import com.hkts.naturalgas.dictionaries.service.IHktsFieldControlService;
import com.hkts.naturalgas.dictionaries.utils.DBUtils;
import com.hkts.naturalgas.dictionaries.vo.PageVo;
import com.hkts.naturalgas.summary.service.ISummaryDictionaryService;
import com.hkts.naturalgas.summary.vo.SummaryVo;
import com.hkts.naturalgas.utils.CustomPage;
import com.hkts.naturalgas.utils.CustomPageUtils;
import com.hkts.naturalgas.utils.Result;
import com.hkts.naturalgas.utils.ResultUtil;
import com.hkts.naturalgas.workshop.entity.HktsWorkShop;
import com.hkts.naturalgas.workshop.service.IHktsWorkShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @ClassNameTableController
 * @Description
 * @Author yck
 * @Date2021-10-08 15:39
 * @Version V1.0
 **/
@RestController
@RequestMapping("/enviroment/tableTitle")
public class TableController {
    @Autowired
    public IHktsEnvironmentProtectionDictionariesService iHktsEnvironmentProtectionDictionariesService;
    @Autowired
    public IHktsEnvironmentProtectionItemService iHktsEnvironmentProtectionItemService;
    @Autowired
    public IHktsWorkShopService iHktsWorkShopService;
    @Autowired
    public IHktsFieldControlService iHktsFieldControlService;
    @Autowired
    public ISummaryDictionaryService iSummaryDictionaryService;

    @Autowired
    public  DBUtils dBUtils;
    /** 功能描述:
    *根据code查当前部门
    * @return:
    * @Author: 杨春科
    * @Date: 2021-10-09 8:39
    * @Description //TODO
    */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getWorkShopByCode")
    public Result<HktsWorkShop> getWorkShopByCode(String code){
        HktsWorkShop shop=  iHktsWorkShopService.getWorkShopByCode(code);
        return ResultUtil.data(shop);
    }


    /** 功能描述:
    *[workShop] 根据车间进行查询所有的表
    * @return: com.hkts.naturalgas.utils.Result<java.util.List<com.hkts.naturalgas.dictionaries.entity.HktsEnvironmentProtectionDictionaries>>
    * @Author: 杨春科
    * @Date: 2021-10-08 15:54
    * @Description //TODO 
    */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getTableByWorkShop")
    public Result<List<HktsEnvironmentProtectionDictionaries>> getTableByWorkShop(String code,String summary){
      List<HktsEnvironmentProtectionDictionaries> workShops=iHktsEnvironmentProtectionDictionariesService.getTableByWorkShop(code,summary);
        return ResultUtil.data(workShops);
    }
    /**/
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getTableItemByTableId")
    public Result<List<HktsEnvironmentProtectionItem>> getTableItemByTableId(String tableId){
        List<HktsEnvironmentProtectionItem> workShops=iHktsEnvironmentProtectionItemService.getTableItemByTableId(tableId);
        return ResultUtil.data(workShops);
    }
    /** 功能描述:
    *  根据tableId，日期 查询数据
    * @return: 
    * @Author: 杨春科
    * @Date: 2021-10-08 16:29
    * @Description //TODO 
    */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getPage")
    public Result<CustomPage> getPage(PageVo vo) throws ParseException {
      CustomPage page=  iHktsEnvironmentProtectionItemService.getPage(vo);
        return ResultUtil.data(page);
    }
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getChart")
    public Result<Map> getChart(PageVo vo) throws ParseException {
        Map map=  iHktsEnvironmentProtectionItemService.getChart(vo);
        return ResultUtil.data(map);
    }
    /** 功能描述:
    *[page, hktsEnvironmentProtectionItem]
    * @return: com.hkts.naturalgas.utils.Result<java.lang.Object>
    * @Author: 杨春科
    * @Date: 2021-12-06 13:55
    * @Description //TODO
     * 设置字段页面的分页
    */
    @ResponseBody
    @GetMapping("/getHktsEnvironmentProtectionItemPageList")
    public Result<Object> getHontUserList(Page<HktsEnvironmentProtectionItem> page, HktsEnvironmentProtectionItem hktsEnvironmentProtectionItem) throws Exception {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iHktsEnvironmentProtectionItemService.selectPage(page, hktsEnvironmentProtectionItem)));
    }
    /** 功能描述:
    *  修改字段设置 设置为开关量固定值或模拟量固定值
    * @return:
    * @Author: 杨春科
    * @Date: 2021-12-06 14:05
    * @Description //TODO
    */
    @ResponseBody
    @PostMapping("/updateProtectionItem")
    public Result<String> updateProtectionItem(@RequestBody HktsFieldControl filed){
        iHktsFieldControlService.updateProtectionItem(filed);
        return ResultUtil.success("修改成功！");
    }
    @ResponseBody
    @GetMapping("/selectFiledControllerByItemId")
    public Result<HktsFieldControl> selectFiledControllerByItemId(String itemId){
        HktsFieldControl  fieldControl=iHktsFieldControlService.selectFiledControllerByItemId(itemId);
        return ResultUtil.data(fieldControl);
    }
    //恢复字段为不需要处理
    @ResponseBody
    @PostMapping("/updateProtectionItemNeedDeal")
    public Result<String> updateProtectionItemNeedDeal(@RequestBody HktsFieldControl filed){
        HktsEnvironmentProtectionItem byId = iHktsEnvironmentProtectionItemService.getById(filed.getItemId());
        byId.setNeedDeal(EnviromentConstance.NOT_DEAL_WITH);
        iHktsEnvironmentProtectionItemService.updateById(byId);
        return ResultUtil.success("修改成功！");
    }
    ///下边接口需要登录校验
    ///登录--------------------------------------
    /** 功能描述:
     *[workShop] 根据车间进行查询所有的表
     * @return: com.hkts.naturalgas.utils.Result<java.util.List<com.hkts.naturalgas.dictionaries.entity.HktsEnvironmentProtectionDictionaries>>
     * @Author: 杨春科
     * @Date: 2021-10-08 15:54
     * @Description //TODO
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getTableByWorkShopLogin")
    public Result<List<HktsEnvironmentProtectionDictionaries>> getTableByWorkShopLogin(String code,String summary){
        List<HktsEnvironmentProtectionDictionaries> workShops=iHktsEnvironmentProtectionDictionariesService.getTableByWorkShop(code,summary);
        return ResultUtil.data(workShops);
    }
    /**/
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getTableItemByTableIdLogin")
    public Result<List<HktsEnvironmentProtectionItem>> getTableItemByTableIdLogin(String tableId){
        List<HktsEnvironmentProtectionItem> workShops=iHktsEnvironmentProtectionItemService.getTableItemByTableId(tableId);
        return ResultUtil.data(workShops);
    }
    /** 功能描述:
     *  根据tableId，日期 查询数据
     * @return:
     * @Author: 杨春科
     * @Date: 2021-10-08 16:29
     * @Description //TODO
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getPageLogin")
    public Result<CustomPage> getPageLogin(PageVo vo) throws ParseException {
        CustomPage page=  iHktsEnvironmentProtectionItemService.getPage(vo);
        return ResultUtil.data(page);
    }
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getChartLogin")
    public Result<Map> getChartLogin(PageVo vo) throws ParseException {
        Map map=  iHktsEnvironmentProtectionItemService.getChart(vo);
        return ResultUtil.data(map);
    }
    /** 功能描述:
     *[page, hktsEnvironmentProtectionItem]
     * @return: com.hkts.naturalgas.utils.Result<java.lang.Object>
     * @Author: 杨春科
     * @Date: 2021-12-06 13:55
     * @Description //TODO
     * 设置字段页面的分页
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getLastRowByTableId")
    public Result<Map> getLastRowByTableId(String tableId) throws ParseException {
        HktsEnvironmentProtectionDictionaries byId = iHktsEnvironmentProtectionDictionariesService.getById(tableId);
        Map<String, Object> lastRow = dBUtils.getLastRowNoCache(byId.getTableName());
        return ResultUtil.data(lastRow);
    }
    @ResponseBody
    @GetMapping("/getHktsEnvironmentProtectionItemPageListLogin")
    public Result<Object> getHontUserListLogin(Page<HktsEnvironmentProtectionItem> page, HktsEnvironmentProtectionItem hktsEnvironmentProtectionItem) throws Exception {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iHktsEnvironmentProtectionItemService.selectPage(page, hktsEnvironmentProtectionItem)));
    }
    /** 功能描述:
     *  修改字段设置 设置为开关量固定值或模拟量固定值
     * @return:
     * @Author: 杨春科
     * @Date: 2021-12-06 14:05
     * @Description //TODO
     */
    @ResponseBody
    @PostMapping("/updateProtectionItemLogin")
    public Result<String> updateProtectionItemLogin(@RequestBody HktsFieldControl filed){
        iHktsFieldControlService.updateProtectionItem(filed);
        return ResultUtil.success("修改成功！");
    }
    @ResponseBody
    @GetMapping("/selectFiledControllerByItemIdLogin")
    public Result<HktsFieldControl> selectFiledControllerByItemIdLogin(String itemId){
        HktsFieldControl  fieldControl=iHktsFieldControlService.selectFiledControllerByItemId(itemId);
        return ResultUtil.data(fieldControl);
    }
    //恢复字段为不需要处理
    @ResponseBody
    @PostMapping("/updateProtectionItemNeedDealLogin")
    public Result<String> updateProtectionItemNeedDealLogin(@RequestBody HktsFieldControl filed){
        HktsEnvironmentProtectionItem byId = iHktsEnvironmentProtectionItemService.getById(filed.getItemId());
        byId.setNeedDeal(EnviromentConstance.NOT_DEAL_WITH);
        iHktsEnvironmentProtectionItemService.updateById(byId);
        return ResultUtil.success("修改成功！");
    }
   //环保统计接口
   @ResponseBody
   @GetMapping("/summary")
   public Result<SummaryVo > summary(String tableId,String date){
       SummaryVo list=   iSummaryDictionaryService.getListByTableIdAndDate(tableId,date);
       return ResultUtil.data(list);
   }

}
